package com.sino.androiddesign.recyclerviewdemo


import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.util.SparseArrayCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView

import com.sino.androiddesign.R
import kotlinx.android.synthetic.main.fragment_header_and_footer.*


/**
 * 通过Adapter 添加头布局和尾布局
 *
 */
class HeaderAndFooterFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_header_and_footer, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        fragment_hf_rv.layoutManager = LinearLayoutManager(context)
        var datas: ArrayList<String> = ArrayList()
        for (i in 0 until 50) {
            datas.add("test$i")
        }
        var testAdapter = TestAdapter(context!!, datas)

        var headerAndFooterAdapter = HeaderAndFooterAdapter(testAdapter)
        var inflate = layoutInflater.inflate(R.layout.fragment_hf_header_layout, fragment_hf_rv, false)
        inflate.setOnClickListener {
            println("dianjia")
            var secView = layoutInflater.inflate(R.layout.fragment_hf_header_layout, fragment_hf_rv, false)
            headerAndFooterAdapter.addFooterView(secView)

            for (i in 0 until 20) {
                datas.add("new$i")
                testAdapter.notifyDataSetChanged()
            }
        }
        headerAndFooterAdapter.addHeaderView(inflate)
        headerAndFooterAdapter.addFooterView(inflate)

        fragment_hf_rv.adapter = headerAndFooterAdapter
    }

}

class TestAdapter(private var context: Context, private var datas: ArrayList<String>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
        var inflate = LayoutInflater.from(context).inflate(R.layout.fragment_hf_item_layout, parent, false)
        var viewHolder = TestViewHolder(inflate)
        viewHolder.bindView(viewHolder.itemView)
        return viewHolder
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is TestViewHolder) {
            holder.textTv!!.text = datas[position]
        }
    }
}

class TestViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
    var textTv: TextView? = null
    fun bindView(itemView: View?) {
        textTv = itemView!!.findViewById(R.id.hf_item_tv)
    }
}

/**
 * 添加自定义的头部和尾部
 */
class HeaderAndFooterAdapter(private var mAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    //头布局
    private var mHeaderViews: SparseArrayCompat<View> = SparseArrayCompat()
    //尾布局
    private var mFooterViews: SparseArrayCompat<View> = SparseArrayCompat()

    //头部类型开始位置   用于viewType
    private var BASE_ITEM_TYPE_HEADER = Math.pow(10.toDouble(), 6.toDouble()).toInt()
    //尾部类型开始位置   用于viewType
    private var BASE_ITEM_TYPE_FOOTER = Math.pow(10.toDouble(), 6.toDouble()).toInt() * 2

    override fun getItemViewType(position: Int): Int {
        if (isHeaderPosition(position)) {
            return mHeaderViews.keyAt(position)
        }

        if (isFooterPosition(position)) {
            var index: Int = position - mAdapter.itemCount - mHeaderViews.size()
            return mFooterViews.keyAt(index)
        }

        var index: Int = position - mHeaderViews.size()
        return mAdapter.getItemViewType(index)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        if (isHeaderViewType(viewType)) {
            var headerView = mHeaderViews.get(viewType)
            return createHeaderAndFooterViewHolder(headerView)
        }
        if (isFooterViewType(viewType)) {
            var view = mFooterViews.get(viewType)
            return createHeaderAndFooterViewHolder(view)
        }
        return mAdapter.onCreateViewHolder(parent, viewType)

    }

    override fun getItemCount(): Int {

        return mHeaderViews.size() + mAdapter.itemCount + mFooterViews.size()
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        if (isHeaderPosition(position) || isFooterPosition(position)) {
            println("是否拦截 $position")
            return
        }

        var indexPosition = position - mHeaderViews.size()
            mAdapter.onBindViewHolder(holder, indexPosition)
//        if (holder is TestViewHolder) {
//            println("是 TestViewHolder")
//        } else {
//            println("拦截失败 $position")
//            println("不是 TestViewHolder")
//        }
    }

    /**
     * 是不是头部位置
     */
    private fun isHeaderPosition(position: Int): Boolean {
        var b = position < mHeaderViews.size()
        println(" 位置 isHeaderPosition: $b")
        return b
    }

    /**
     * 是不是尾部
     */
    private fun isFooterPosition(position: Int): Boolean {
        var b = position >= mAdapter.itemCount + mHeaderViews.size()
        println(" 位置 isFooterPosition: $b")
        return b
    }

    /**
     * 判断是不是 头部类型
     */
    private fun isHeaderViewType(viewType: Int): Boolean {
        var indexOfKey = mHeaderViews.indexOfKey(viewType)
        return indexOfKey >= 0
    }

    /**
     * 判断是不是 尾部类型
     */
    private fun isFooterViewType(viewType: Int): Boolean {
        var indexOfKey = mFooterViews.indexOfKey(viewType)
        return indexOfKey >= 0
    }


    /**
     * 創建头部或尾部的holder
     */
    private fun createHeaderAndFooterViewHolder(view: View): RecyclerView.ViewHolder {
        return object : RecyclerView.ViewHolder(view) {}
    }

    /**
     * 添加头部
     */
    fun addHeaderView(view: View) {
        var position = mHeaderViews.indexOfValue(view)
        if (position < 0) {  //该view 不存在
            mHeaderViews.put(BASE_ITEM_TYPE_HEADER++, view)
            notifyDataSetChanged()
            println("addHeaderView")
        }
    }

    /**
     * 移除头部
     */
    fun removeHeaderView(view: View) {
        var position = mHeaderViews.indexOfValue(view)
        if (position < 0) {
            mHeaderViews.removeAt(position)
            notifyDataSetChanged()
        }
    }

    /**
     * 添加尾部
     */
    fun addFooterView(view: View) {
        var position = mFooterViews.indexOfValue(view)
        if (position < 0) {
            mFooterViews.put(BASE_ITEM_TYPE_FOOTER++, view)
            notifyDataSetChanged()
            println("addFooterView")
        }
    }

    /**
     * 移除尾部
     */
    fun removeFooterView(view: View) {
        var position = mFooterViews.indexOfValue(view)
        if (position < 0) {
            mFooterViews.removeAt(position)
            notifyDataSetChanged()
        }
    }
}
